﻿using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using WebServerConnectorDll;

namespace FruitManagerApp
{
    public class UserAddViewModel : NotificationObject
    {
        private UserAdd gWindow;
        private UserViewModel gUserViewModel;

        public UserAddViewModel(UserAdd pWindow, UserViewModel pUserViewModel)
        {
            gWindow = pWindow;
            gUserViewModel = pUserViewModel;

            Initialize();
        }

        public void Initialize()
        {
            HttpResponse response = API.SelectProvince();
            if (response.S == HttpResponse.Success)
            {
                Province = JSON.parse<ObservableCollection<CProvince>>(response.D);
            }
        }

        private String uid;
        public String UID
        {
            get
            {
                return uid;
            }

            set
            {
                uid = value;
                RaisePropertyChanged("UID");
            }
        }

        private String name;
        public String Name
        {
            get
            {
                return name;
            }

            set
            {
                name = value;
                RaisePropertyChanged("Name");
            }
        }

        private String username;
        public String Username
        {
            get
            {
                return username;
            }

            set
            {
                username = value;
                RaisePropertyChanged("Username");
            }
        }

        private String password;
        public String Password
        {
            get
            {
                return password;
            }

            set
            {
                password = value;
                RaisePropertyChanged("Password");
            }
        }

        private String passwordEnter;
        public String PasswordEnter
        {
            get
            {
                return passwordEnter;
            }

            set
            {
                passwordEnter = value;
                RaisePropertyChanged("PasswordEnter");
            }
        }

        private ObservableCollection<CProvince> province = new ObservableCollection<CProvince>();
        public ObservableCollection<CProvince> Province
        {
            get
            {
                return province;
            }

            set
            {
                province = value;
                RaisePropertyChanged("Province");
            }
        }

        private int provinceUID;
        public int ProvinceUID
        {
            get
            {
                return provinceUID;
            }

            set
            {
                provinceUID = value;
                RaisePropertyChanged("ProvinceUID");
            }
        }

        private ObservableCollection<CCity> city = new ObservableCollection<CCity>();
        public ObservableCollection<CCity> City
        {
            get
            {
                return city;
            }

            set
            {
                city = value;
                RaisePropertyChanged("City");
            }
        }

        private int cityUID;
        public int CityUID
        {
            get
            {
                return cityUID;
            }

            set
            {
                cityUID = value;
                RaisePropertyChanged("CityUID");
            }
        }

        private ObservableCollection<COffice> office = new ObservableCollection<COffice>();
        public ObservableCollection<COffice> Office
        {
            get
            {
                return office;
            }

            set
            {
                office = value;
                RaisePropertyChanged("Office");
            }
        }

        private int officeUID;
        public int OfficeUID
        {
            get
            {
                return officeUID;
            }

            set
            {
                officeUID = value;
                RaisePropertyChanged("OfficeUID");
            }
        }

        private ObservableCollection<CDevice> device = new ObservableCollection<CDevice>();
        public ObservableCollection<CDevice> Device
        {
            get
            {
                return device;
            }

            set
            {
                device = value;
                RaisePropertyChanged("Device");
            }
        }

        private int deviceUID;
        public int DeviceUID
        {
            get
            {
                return deviceUID;
            }

            set
            {
                deviceUID = value;
                RaisePropertyChanged("DeviceUID");
            }
        }

        private ObservableCollection<CCoupon> coupon = new ObservableCollection<CCoupon>();
        public ObservableCollection<CCoupon> Coupon
        {
            get
            {
                return coupon;
            }

            set
            {
                coupon = value;
                RaisePropertyChanged("Coupon");
            }
        }

        private int couponUID;
        public int CouponUID
        {
            get
            {
                return couponUID;
            }

            set
            {
                couponUID = value;
                RaisePropertyChanged("CouponUID");
            }
        }

        private int integral;
        public int Integral
        {
            get
            {
                return integral;
            }

            set
            {
                integral = value;
                RaisePropertyChanged("Integral");
            }
        }

        protected DelegateCommand<object> saveCommand;
        public DelegateCommand<object> SaveCommand
        {
            get
            {
                return saveCommand ?? (saveCommand = new DelegateCommand<object>(ExcuteSaveCommand));
            }
        }

        protected virtual void ExcuteSaveCommand(object obj)
        {
            List
            int defAddressUID = 0;
            foreach (var control in gWindow.Addresses.Children)
            {
                if (control is RadioButton)
                {
                    if (((RadioButton)control).IsChecked == true)
                    {
                        defAddressUID = Convert.ToInt16(((RadioButton)control).Uid);
                        break;
                    }
                }
            }

            API.InsertDevice
            API.InsertUser(new CUser(UID, Name, Username, Password, OfficeUID, Integral, defAddressUID));
            gUserViewModel.UpdateList();
            gWindow.Close();
        }

        protected DelegateCommand<object> quitCommand;
        public DelegateCommand<object> QuitCommand
        {
            get
            {
                return quitCommand ?? (quitCommand = new DelegateCommand<object>(ExcuteQuitCommand));
            }
        }

        protected virtual void ExcuteQuitCommand(object obj)
        {
            gWindow.Close();
        }

        protected DelegateCommand<object> _provinceChanged;
        public DelegateCommand<object> _ProvinceChanged
        {
            get
            {
                return _provinceChanged ?? (_provinceChanged = new DelegateCommand<object>(ProvinceChanged));
            }
        }

        protected virtual void ProvinceChanged(object obj)
        {
            City.Clear();
            Office.Clear();
            Device.Clear();
            HttpResponse response = API.SelectCity(ProvinceUID);
            if (response.S == HttpResponse.Success)
            {
                City = JSON.parse<ObservableCollection<CCity>>(response.D);
            }
        }

        protected DelegateCommand<object> _cityChanged;
        public DelegateCommand<object> _CityChanged
        {
            get
            {
                return _cityChanged ?? (_cityChanged = new DelegateCommand<object>(CityChanged));
            }
        }

        protected virtual void CityChanged(object obj)
        {
            Office.Clear();
            Device.Clear();
            HttpResponse response = API.SelectOffice(ProvinceUID, CityUID);
            if (response.S == HttpResponse.Success)
            {
                Office = JSON.parse<ObservableCollection<COffice>>(response.D);
            }
        }

        protected DelegateCommand<object> _officeChanged;
        public DelegateCommand<object> _OfficeChanged
        {
            get
            {
                return _officeChanged ?? (_officeChanged = new DelegateCommand<object>(OfficeChanged));
            }
        }

        protected virtual void OfficeChanged(object obj)
        {
            Device.Clear();
            HttpResponse response = API.SelectDevice(OfficeUID);
            if (response.S == HttpResponse.Success)
            {
                Device = JSON.parse<ObservableCollection<CDevice>>(response.D);
            }
        }

        protected DelegateCommand<object> _addAddress;
        public DelegateCommand<object> _AddAddress
        {
            get
            {
                return _addAddress ?? (_addAddress = new DelegateCommand<object>(AddAddress));
            }
        }

        protected virtual void AddAddress(object obj)
        {
            RadioButton button = new RadioButton();
            button.Uid = DeviceUID.ToString();
            foreach (var item in Device)
            {
                if (DeviceUID == item.UID)
                {
                    button.Content = item.Address;
                    break;
                }
            }
            gWindow.Addresses.Children.Add(button);
        }
    }
}
